/*
 * CollectShopFragment.java
 * business
 *
 * Created by SunDing on 2016/9/9.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.collect.shop;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.yidiandao.business.R;
import com.yidiandao.business.base.SuperBaseFragment;
import com.yidiandao.business.data.event.CollectShopEvent;
import com.yidiandao.business.ui.adapter.CollectShopAdapter;
import com.yidiandao.business.ui.shop.ShopActivity;
import com.yidiandao.business.ui.widget.listview.DisplayListItem;
import com.yidiandao.business.utils.UIUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import in.srain.cube.views.loadmore.LoadMoreContainer;
import in.srain.cube.views.loadmore.LoadMoreHandler;
import in.srain.cube.views.loadmore.LoadMoreListViewContainer;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;

/** 我的 收藏的商店. */
public class CollectShopFragment extends SuperBaseFragment implements CollectShopContract.View {
    private static final String TAG = CollectShopFragment.class.getSimpleName();
    CollectShopContract.Presenter mPresenter;
    CollectShopAdapter mCollectShopAdapter;
    private ListView mListView;
    private TextView mTvRandomGo;
    private PtrClassicFrameLayout mPtrFrame;
    private LoadMoreListViewContainer mLoadMoreContainer;

    public static CollectShopFragment getInstance() {
        return new CollectShopFragment();
    }

    @Override
    protected void initViews() {

        mTvRandomGo = (TextView) mRootView.findViewById(R.id.tv_random_go);
        mLoadMoreContainer =
                (LoadMoreListViewContainer) mRootView.findViewById(R.id.load_more_container);

        mPtrFrame = (PtrClassicFrameLayout) mRootView.findViewById(R.id.ptr_frame);
        mListView = (ListView) mRootView.findViewById(R.id.list_view);

        mCollectShopAdapter = new CollectShopAdapter(mActivity);
        mListView.setAdapter(mCollectShopAdapter);

        initEvent();
    }

    private void initEvent() {
        mPtrFrame.setLastUpdateTimeRelateObject(this);
        // 下拉刷新
        mPtrFrame.setPtrHandler(
                new PtrDefaultHandler() {
                    @Override
                    public boolean checkCanDoRefresh(
                            PtrFrameLayout frame, View content, View header) {
                        return PtrDefaultHandler.checkContentCanBePulledDown(
                                frame, mListView, header);
                    }

                    @Override
                    public void onRefreshBegin(PtrFrameLayout frame) {
                        mPresenter.start();
                    }
                });

        // 上拉加载更多
        mLoadMoreContainer.useDefaultFooter();
        mLoadMoreContainer.setLoadMoreHandler(
                new LoadMoreHandler() {
                    @Override
                    public void onLoadMore(LoadMoreContainer loadMoreContainer) {
                        mPresenter.getCollectionData(false);
                    }
                });
        mTvRandomGo.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ShopActivity.actionStart(mActivity);
                    }
                });
    }

    @Override
    protected void startLoadData() {
        mPresenter.start();
    }

    @Override
    public String getPagerTag() {
        return TAG;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_collect_shop;
    }

    @Override
    public void setPresenter(CollectShopContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public void showToast(String text) {
        UIUtils.showToast(text);
    }

    @Override
    public void refreshContent(int result) {
        superRefreshLayoutState(result);
    }

    @Override
    public int checkResultObject(Object resultObject) {
        return superCheckListState(resultObject);
    }

    @Override
    public void setDisplayListItems(List<DisplayListItem> displayListItems) {
        mCollectShopAdapter.setData(displayListItems);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void collectShop(CollectShopEvent event) {
        mPresenter.collectShop(event);
    }

    @Override
    public void setRefreshComplete() {
        mPtrFrame.refreshComplete();
    }

    @Override
    public void loadMoreFinish(boolean emptyResult, boolean hasMore) {
        mLoadMoreContainer.loadMoreFinish(emptyResult, hasMore);
    }

    @Override
    public void loadMoreError() {
        mLoadMoreContainer.loadMoreError(0, UIUtils.getString(R.string.load_more_failed));
    }

    @Override
    public void onResume() {
        super.onResume();
        mPresenter.start();
    }
}
